package net.yebaihe.wc;

import java.util.HashMap;

import net.yebaihe.wc.DataMan.MSGTYPE;

import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Login extends StateMachine {
	EditText edtEmail;
	EditText edtPasswd;
	

	@Override
	protected int getLayoutRes() {
		return R.layout.login;
	}

	@Override
	protected void prepareClient(View client) {
		edtEmail=(EditText) client.findViewById(R.id.edtEmail);
		edtPasswd=(EditText) client.findViewById(R.id.edtPasswd);
		Button btnLogin=(Button) client.findViewById(R.id.btnlogin);
		btnLogin.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				edtEmail.setEnabled(false);
				edtPasswd.setEnabled(false);
				DataMan.get(Login.this).login(edtEmail.getText().toString(),edtPasswd.getText().toString());
			}
		});
		
		Button btnRegister=(Button) client.findViewById(R.id.btnRegister);
		btnRegister.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				Intent intent=new Intent(Login.this,Register.class);
				startActivity(intent);
				overridePendingTransition(R.anim.left_in,R.anim.left_out); 
			}
		});
		
	}

	@Override
	protected String getTopbarTitle() {
		return "登录";
	}

	@Override
	protected void HandleMessage(MSGTYPE register, boolean ret,
			HashMap<String, String> params) {
		switch (register){
		case LOGIN:
			if (ret){
				Toast.makeText(this, "登录成功！", Toast.LENGTH_LONG).show();
				getWCAppContext().email=edtEmail.getText().toString();
				Intent intent=new Intent(Login.this,Index.class);
				startActivity(intent);
				finish();
				overridePendingTransition(R.anim.up_in,R.anim.up_out); 
			}
			else{
				Toast.makeText(this, "登录失败！", Toast.LENGTH_LONG).show();
				edtEmail.setEnabled(true);
				edtPasswd.setEnabled(true);
			}
			break;
		}
	}

}
